<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="900" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
			<el-form-item label="工序名" prop="name">
				<el-input v-model="form.name" placeholder="请输入工序名" clearable></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="remake">
				<el-input v-model="form.remake" placeholder="请输入备注" clearable></el-input>
			</el-form-item>
			<el-form-item label="总工价" prop="">
				{{priceTotal}} 元
			</el-form-item>
			<el-form-item label="模板" prop="">			
			    <el-checkbox-group v-model="checkboxGroup1" size="large">
			    	<el-checkbox-button v-for="tmp in tmps" :key="tmp.id" :label="tmp.name">
			        {{ tmp.name }}
			    	</el-checkbox-button>
			    </el-checkbox-group>
			</el-form-item>
			<el-form-item label="快捷添加" prop="">
				<el-button type="primary" icon="el-icon-check" @click="tmpChange" >添加选中</el-button>
				<el-button icon="el-icon-plus" @click="addProcess('')" type="primary">添加空白</el-button>
			</el-form-item>
			<el-form-item label="改往期工资" prop="">
				<el-switch v-model="form.old_update" />
				从开工日开始修改
			</el-form-item>
			<el-form-item label="工序表" prop="">
				<scTable id="table_process" ref="table" row-key="id" hidePagination="true" hideDo="true" :data="form.processes" border style="wdith: 100%; min-height: 100px;">
				    <el-table-column prop="name" label="工序名"	width="120">
				    	<template #default="scope">
							<el-input v-if="mode == 'add'" v-model="scope.row.name" />
							<el-input v-if="mode == 'edit'" disabled v-model="scope.row.name" />
						</template>
					</el-table-column>
				    <el-table-column prop="price" label="工价" style="padding: 0px;" width="100">
				    	<template #default="scope">
							<el-input type="number" @change="priceChange" v-model="scope.row.price"  />
						</template>
				    </el-table-column>
				    <el-table-column  prop="is_add" label="操作" width="60" >
				    	<template #default="scope">
							<el-button icon="el-icon-delete" @click="delProcess(scope.row)" type="danger"  v-model="scope.row.price" circle />
						</template>
				    </el-table-column>
				</scTable>
			</el-form-item>

			<el-form-item v-for="item in all_sku" :label="item.sku_name" :key="item.id" prop="">
				<div  v-if="item.sku_name == '颜色'">
				<el-checkbox-group   @change="checkSku" v-model="checkColor">
					<div>
					<el-checkbox border v-for="element in item.sku_values" :key="element.id" :label="element.value" >{{element.value}}</el-checkbox>
					</div>
					<el-button @click="skuAdd(item)" icon="el-icon-plus">添加颜色</el-button>
				</el-checkbox-group>
				</div>

				<div  v-if="item.sku_name == '尺码'">
				<el-checkbox-group   @change="checkSku" v-if="item.sku_name == '尺码'" v-model="checkSize">
					<div>
					<el-checkbox border v-for="element in item.sku_values" :key="element.id" :label="element.value" >{{element.value}}</el-checkbox>
					</div>
					<el-button @click="skuAdd(item)" icon="el-icon-plus">添加尺码</el-button>
				</el-checkbox-group>
				</div>
			</el-form-item>
			
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

	<sku-dialog v-if="dialog.sku" ref="skuDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></sku-dialog>
</template>

<script>
	import skuDialog from './sku'
	import cloneDeep from 'lodash/cloneDeep'
	export default {
		emits: ['success', 'closed'],
		components: {
			skuDialog
		},
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				dialog: {
					sku: false
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					old_update: false,
					name: '',
					remake: '',
					processes: [
					{
						id: this.$TOOL.generateUUID(),
						procedure_id: "",
						price: '',
						name: name,
						is_add: 1
					}]
				},
				tmps: [],
				all_sku: [],
				checkColor:[],
				checkSize:[],
				checkboxGroup1: [],
				priceTotal: 0,
				//验证规则
				rules: {
					name: [
						{required: true, message: '请输入姓名'}
					]
				},
				//所需数据选项
				groups: [],
				groupsProps: {
					value: "id",
					emitPath: false,
					checkStrictly: true
				}
			}
		},
		mounted() {
			this.getTmp();

		},
		methods: {
			tmpChange() {
				for (var i in this.form.processes) {
					var del_index = this.checkboxGroup1.indexOf(this.form.processes[i].name)
					if (del_index !== -1) {
						this.checkboxGroup1.splice(del_index, 1)
					}
				}

				for (var j in this.checkboxGroup1) {
					this.addProcess(this.checkboxGroup1[j]);
				}
				console.log(this.checkboxGroup1)
			},
			async getTmp() {
				var res = await this.$API.procedure.tmp.get()

				if (res.code == 200) {
					this.$nextTick(() => {
						this.tmps = res.data
					})
				}

				// 获取颜色尺码
				// var sku_res = await this.$API.sku.list.get()
				// if (sku_res.code == 200) {
				// 	this.all_sku = sku_res.data
				// }
			},

			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			async submit(){
				var valid = await this.$refs.dialogForm.validate().catch(() => {})
				if(!valid){
					return false
				}
				this.isSaveing = true;
				this.form.mode = this.mode
				this.form.checkSize = this.checkSize
				this.form.checkColor = this.checkColor
				var res = await this.$API.procedure.save.post(this.form);
				this.isSaveing = false;
				if(res.code == 200){
					if(this.mode=='add'){
						this.form.id = res.data
					}
					this.$emit('success', this.form, this.mode)
					this.visible = false;
					this.$message.success("操作成功")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
			},

			// 添加工序
			addProcess(name = '') {

				this.form.processes.push({
					id: this.$TOOL.generateUUID(),
					procedure_id: this.form.id,
					price: '',
					name: name,
					is_add: 1

				})
			},
			delProcess(row) {
				this.$refs.table.removeKeys([row.id])
			},
			skuAdd(row) {
				this.dialog.sku = true
				this.$nextTick(() => {
					this.$refs.skuDialog.open().setData(row)
				})
			},
			//  sku 添加成功后
			handleSaveSuccess(data, mode) {
				if (mode == "add") {
					for (var i in this.all_sku) {
						if (this.all_sku[i].id == data.sku_id) {
							this.all_sku[i].sku_values.push(data)
						}
					}
				}
			},
			//表单注入数据
			setData(data){

				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				
				this.form = cloneDeep(data)
				this.form.old_update = false
				for (var i in this.form.processes) {
					this.priceTotal = parseFloat(this.priceTotal) + parseFloat(this.form.processes[i].price)
				}
				this.priceTotal = this.priceTotal.toFixed(3)
				console.log(this.form)
				if (this.form.processes.length == 0) {
					this.addProcess()
				}
			},
			priceChange() {
				this.priceTotal = 0
				for (var i in this.form.processes) {
					this.priceTotal = parseFloat(this.priceTotal) + parseFloat(this.form.processes[i].price)
				}
				this.priceTotal = this.priceTotal.toFixed(3)
			},
		}
	}
</script>

<style>
</style>
